﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Hyunhdai_Saigon_Project.uc
{
    public partial class uc_admin_baiviet : System.Web.UI.UserControl
    {
        private String sql;
        private String id;
        private String caid;

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString.Get("id");
            caid = Request.QueryString.Get("caid");
            try
            {
                drlCat.SelectedIndex = int.Parse(caid);
            }
            catch (Exception)
            {
                caid = "0";
            }
            sql = "select * from articles where id = " + id;
            if (id != null && !IsPostBack)
                bindData();
            img.ImageUrl = txtImgUrl.Text;
            txtDate.Text = DateTime.Now.ToString();

        }

        protected void bindData()
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisaigonConnectionString"].ConnectionString);
            connection.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            string title = dt.Rows[0][2].ToString();
            string introtext = dt.Rows[0][3].ToString();
            string ftext = dt.Rows[0][4].ToString();
            string img = dt.Rows[0][5].ToString();
            string date = dt.Rows[0][6].ToString();
            txtTitle.Text = title;
            fckMoTa.Value = introtext;
            fckNoiDung.Value = ftext;
            connection.Close();
        }

        protected void saveToDB(String title, String introtext, String fulltext, String img)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisaigonConnectionString"].ConnectionString);
            String sql = "insert into articles values('" + title + "','" + introtext + "','" + fulltext + "','" + img + ")";
            // String sql = "insert into articles values('1','1','1','')";
            SqlCommand command = new SqlCommand(sql, connection);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();
        }

        protected void submit_Click(object sender, EventArgs e)
        {
          
                int ncaid = drlCat.SelectedIndex;
                if (id == null)
                {
                    News_EditData(0, ncaid, txtTitle.Text + "",
                   fckMoTa.Value + "",
                   fckNoiDung.Value + "", txtImgUrl.Text,
                   DateTime.Now);
                }
                else
                {
                    News_EditData(int.Parse(id), ncaid, txtTitle.Text + "",
                  fckMoTa.Value + "",
                  fckNoiDung.Value + "", txtImgUrl.Text,
                  DateTime.Now);
                }
                lblXacNhan.Text = "đã thêm thông tin bài mới";
        
        }
        private void News_EditData(
       int id,
            int caid,
       string title,
       string introtext,
       string ftext,
            string img,
       DateTime date)
        {
            // Khai báo chuỗi kết nối
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisaigonConnectionString"].ConnectionString);
            try
            {
                // Mở kết nối
                connection.Open();
                SqlCommand cmdUpd = new SqlCommand("proc_new_articles", connection);
                cmdUpd.CommandType = CommandType.StoredProcedure;
                cmdUpd.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
                cmdUpd.Parameters.Add(new SqlParameter("@caid", SqlDbType.Int));
                cmdUpd.Parameters.Add(new SqlParameter("@title", SqlDbType.NVarChar));
                cmdUpd.Parameters.Add(new SqlParameter("@introtext", SqlDbType.NText));
                cmdUpd.Parameters.Add(new SqlParameter("@ftext", SqlDbType.NText));
                cmdUpd.Parameters.Add(new SqlParameter("@img", SqlDbType.NVarChar));
                cmdUpd.Parameters.Add(new SqlParameter("@date", SqlDbType.DateTime));
                cmdUpd.Parameters[0].Value = id;
                cmdUpd.Parameters[1].Value = caid;
                cmdUpd.Parameters[2].Value = title;
                cmdUpd.Parameters[3].Value = introtext;
                cmdUpd.Parameters[4].Value = ftext;
                cmdUpd.Parameters[5].Value = img;
                cmdUpd.Parameters[6].Value = date;
                cmdUpd.ExecuteNonQuery();

            }
            catch { }
            finally
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
                connection.Dispose();
            }
        }
        protected void DangXuat_Click(object sender, EventArgs e)
        {
            Session.RemoveAll();
            Response.Redirect("Default.aspx");
        }

        protected void ThemSp_Click(object sender, EventArgs e)
        {
            Response.Redirect("admin.aspx");
        }

        protected void BrowseImage_Click(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            HttpPostedFile files = FileUpload1.PostedFile;
            if (FileUpload1.HasFile == false && files.ContentLength > 500000)
            {
                Label5.Text = "Ảnh không hợp lệ";
            }
            else
            {
                try
                {
                    string txtpath = Server.MapPath("~/assets/images/" + FileUpload1.FileName);
                    FileUpload1.SaveAs(txtpath);
                    txtImgUrl.Text = "assets/images/" + FileUpload1.FileName.ToString();
                    Label5.Text = "Upload thành công";
                }
                catch
                {
                    Label5.Text = "Trùng tên hoặc chưa chọn hình";
                }
            }  
        }
    }
}